﻿/*ydl 2011.5.6
验证页面信息 如：必填 ，正则表达式
如出错，弹出其父节点的上一个<td>innerText 信息--定制弹出
使用方法：调用submitValidatePage() 表单验证通过 返回true，否则 返回false
*/


var ifValidate = true;  //true 验证通过 

function submitValidatePage() {
    ifValidate = true;
    var arrTagType = new Array();
    arrTagType = ["input", "select", "textarea"];
    for (var i = 0; i < arrTagType.length; i++) {
        if (ifValidate) {
            validateControls(arrTagType[i]);
        }
        else {
            return ifValidate;
        }
    }
    return ifValidate;
}

function validateControls(tagType) {
    var pageControl = document.getElementsByTagName(tagType)
    if (tagType == 'input' && ifValidate) {
        for (var i = 0; i < pageControl.length; i++) {
            if (ifValidate == false) {
                return;
            }
            var tempControl = pageControl[i];
            inputBranchCheck(tempControl);
        }
    }
    else if (tagType == 'select' && ifValidate) {
        for (var i = 0; i < pageControl.length; i++) {
            if (ifValidate == false) {
                return;
            }
         //   var pageControl = pageControl[i];
            validateSelectType(pageControl[i]);
        }
    }
    else if (tagType == 'textarea' && ifValidate) {
        for (var i = 0; i < pageControl.length; i++) {
            if (ifValidate == false) {
                return;
            }
           // var pageControl = pageControl[i];
            validateTextareaType(pageControl[i]);
        }
    }
    else {
        return; //
    }

}


//验证TextBox
function checkingTextBox(element) {
    //;
    try {
        var tempElement = element;
        if (ifValidate) {
            validate(element);
        }
        else {
            return;
        }
    }
    catch (e) {

    }
}
//验证password
function checkingPassword(element) {
    try {
        if (ifValidate == false) {
            return;
        }
        validate(element);

    } catch (e) {

    }
    finally { }
}


//验证checkBox
function checkingCheckbox(element) { //同 checkingRadio
    //    ;
    try {
        if (element.name != "" && ifValidate) {
            var musted = element.attributes["musted"]; //必填写
            if (musted && musted.value == "true") {
                var newRaidoArr = document.getElementsByName(element.name);
                var ifSelectRadioArr = false;
                for (var i = 0; i < newRaidoArr.length; i++) {
                    if (newRaidoArr[i].checked) {
                        ifSelectRadioArr = true;
                        return;
                    }
                }
                if (ifSelectRadioArr == false) {
                    ifValidate = false;
                    alertMessage(element, "选择项为必填！");
                    return;
                }
            }
        }
        else {
            return;
        }

    } catch (e) {

    }
    finally { }
}

//验证Radio
function checkingRadio(element) {
    try {
        if (element.name != "" && ifValidate) {
            //            ;
            var musted = element.attributes["musted"];
            if (musted && musted.value.toLocaleString() == "true") {
                var newRaidoArr = document.getElementsByName(element.name);
                var ifSelectRadioArr = false;
                for (var i = 0; i < newRaidoArr.length; i++) {
                    if (newRaidoArr[i].checked) {
                        ifSelectRadioArr = true;
                        return;
                    }
                }
                if (ifSelectRadioArr == false) {
                    ifValidate = false;
                    alertMessage(element, "选择项为必填！");
                    return;
                }

            }
        }
        else {
            return;
        }

    } catch (e) {

    }
    finally { }
}

//验证file  
function checkingFile(element) {
    try {
        var tempElement = element;
        if (ifValidate == false) {
            return;
        }
        validate(element);
    } catch (e) {

    }
    finally { }
}
//input分支验证
function inputBranchCheck(element) {
    switch (element.type) {
        case "text": if (ifValidate) { checkingTextBox(element); } break;
        case "password": if (ifValidate) { checkingPassword(element); } break;
        case "checkbox": if (ifValidate) { checkingCheckbox(element); } break;
        case "radio": if (ifValidate) { checkingRadio(element); } break;
        case "file": if (ifValidate) { checkingFile(element); } break;
    }
}

//通用input控件属性验证   现在只验证 了musted 和正则表达式属性
function validate(element) {
    //;
    var musted = element.attributes["musted"]; //必填写
    var validPatern = element.attributes["validpattern"]; //正则
    if (musted && musted.value == "true" && element.value.replace(/\s/g, '') == "" && ifValidate) {
        alertMessage(element, "为必填项！");
        element.focus();
        ifValidate = false;
        return;
    }
    else if (validPatern && validPatern.value != "" && ifValidate) {//正则验证
        var reg = new RegExp(validPatern.value);
        if (!reg.test(element.value)) {
            alertMessage(element, "填写内容类型不符合，未能通过验证！");
            element.focus();
            ifValidate = false;
            return;
        }
    }
    else { }

}

//验证select 
function validateSelectType(element) {
    try {
        if (ifValidate) {
            var musted = element.attributes["musted"];
            if (musted && musted.value == "true") {
                if (element.selectedIndex < 1) {
                    alertMessage(element, "选择框为必填！");
                    element.focus();
                    ifValidate = false;
                    return;
                }
            }
        }
        else {
            return;
        }
    }
    catch (e) {

    }
}

//独立验证textArea
function validateTextareaType(element) {
    try {
        if (ifValidate) {
            var musted = element.attributes["musted"]; //必填写
            if (musted && musted.value.toLocaleString() == "true" && element.value.replace(/\s/g, '') == "" && ifValidate) {
                alertMessage(element, "文本域为必填！");
                element.focus();
                ifValidate = false;
                return;
            }
        }
        else {
            return;
        }
    }
    catch (e) {
    }
}

//提示信息
function alertMessage(element, message) {
    alert(element.attributes["ctitle"].value + " : " + message);
}